﻿<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="../helpproject.xsl" ?>
<topic template="Default" lasteditedby="Geert" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../helpproject.xsd">
  <title translate="true">Message mediator</title>
  <body>
    <header>
      <para styleclass="Heading1"><text styleclass="Heading1" translate="true">Message mediator</text></para>
    </header>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">Catel allows sending messages to unknown targets by implementing the </text><link displaytype="text" defaultstyle="true" type="weblink" href="http://en.wikipedia.org/wiki/Mediator_pattern" target="_blank" styleclass="Normal" translate="true">mediator pattern</link><text styleclass="Normal" translate="true">. The mediator is assured memory leak free, and can be used safely in any .NET environment (even ASP.NET). Below are a few usage examples of the </text><link displaytype="text" defaultstyle="true" type="topiclink" href="T_Catel_Messaging_MessageMediator" styleclass="Normal" translate="true">MessageMediator</link><text styleclass="Normal" translate="true"> class.</text></para>
    <para styleclass="Heading1"><text styleclass="Heading1" translate="true">Registering to a message</text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">To register a handler for a specific message type, in this case a string, use the following code:</text></para>
    <para styleclass="Code Example"><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">var</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true"> </text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">mediator</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true"> </text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">=</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true"> </text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">ServiceLocator.Instance.ResolveType&lt;IMessageMediator&gt;();</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">mediator.Register&lt;</text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; color:#000000;" translate="true">string</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">&gt;(</text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; color:#000000;" translate="true">this</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">,</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true"> </text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">OnMessage);</text></para>
    <para styleclass="Heading1"><text styleclass="Heading1" translate="true">Sending out a message</text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">To send a message to all recipients, use the following code:</text></para>
    <para styleclass="Code Example"><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">var</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true"> </text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">mediator</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true"> </text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">=</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true"> </text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">ServiceLocator.Instance.ResolveType&lt;IMessageMediator&gt;();</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">mediator.SendMessage&lt;</text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; color:#000000;" translate="true">string</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">&gt;(&quot;message&quot;);</text></para>
    <para styleclass="Heading1"><text styleclass="Heading1" translate="true">Sending out a message with a tag</text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">Sometimes, you want to send messages only based on a tag. For example, you want to let other view models know that you just added a person. All recipients that registered to the </text><text styleclass="Normal" style="font-style:italic;" translate="true">string</text><text styleclass="Normal" style="font-style:normal;" translate="true"> message type with the </text><text styleclass="Normal" style="font-style:italic;" translate="true">Person</text><text styleclass="Normal" style="font-style:normal;" translate="true"> tag will receive the message:</text></para>
    <para styleclass="Code Example"><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">var</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true"> </text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">mediator</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true"> </text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">=</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true"> </text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">ServiceLocator.Instance.ResolveType&lt;IMessageMediator&gt;();</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">mediator.SendMessage&lt;</text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; color:#000000;" translate="true">string</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">&gt;(&quot;Person added&quot;, &quot;Person&quot;);</text></para>
  </body>
</topic>
